import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;

public class LeaderBoard {
	
	private JFrame leaderFrame;
	
	
	public LeaderBoard(){
		displayLeaderBoard();
	}
	
	public void create(){
		
	}

	public void displayLeaderBoard(){
		leaderFrame = new JFrame();
		//leaderFrame.setVisible(true);
		leaderFrame.setBounds(100, 100, 350, 300);
		leaderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JPanel leaderTitlePanel = new JPanel();
		leaderFrame.getContentPane().add(leaderTitlePanel, BorderLayout.NORTH);
		JLabel leaderTitle = new JLabel("Leaderboard");
		leaderTitle.setFont(new Font("Serif", Font.PLAIN, 24)); 
		leaderTitle.setBounds(0, 0, 60, 60);
		leaderTitlePanel.add(leaderTitle);
		
		JPanel centerLeader = new JPanel();
		leaderFrame.add(centerLeader, BorderLayout.CENTER);
		centerLeader.setLayout(null);
		
		JLabel firstPlace = new JLabel("1.");
		firstPlace.setBounds(50, 50, 14, 14);
		centerLeader.add(firstPlace);
		
		JLabel secondPlace = new JLabel("2.");
		secondPlace.setBounds(50, 75, 14, 14);
		centerLeader.add(secondPlace);
		
		JLabel thirdPlace = new JLabel("3.");
		thirdPlace.setBounds(50, 100, 14, 14);
		centerLeader.add(thirdPlace);
		
		JLabel forthPlace = new JLabel("4.");
		forthPlace.setBounds(50, 125, 14, 14);
		centerLeader.add(forthPlace);
		
		JLabel fifthPlace = new JLabel("5.");
		fifthPlace.setBounds(50, 150, 14, 14);
		centerLeader.add(fifthPlace);
		
		JLabel name[] = new JLabel[5];
		JLabel score[] = new JLabel[5];
		int scores[] = new int[5];
		String names[] = new String[5];
		
		 File file = new File("leaderboard.txt");
		 int i =0;
	        try {
	 
	            Scanner scanner = new Scanner(file);
	 
		        for(i=0; i<5;i++){
	                names[i] = scanner.nextLine();
	                name[i] = new JLabel(names[i]);
	                scores[i] = scanner.nextInt();
	                if(scanner.hasNextLine()){
	                	scanner.nextLine();
	                }
	                score[i] = new JLabel(Integer.toString(scores[i]));
	                name[i].setBounds(75, 50 + (i*25), 100, 14);
	                score[i].setBounds(250, 50 + (i*25), 60, 14);
	                centerLeader.add(name[i]);
	                centerLeader.add(score[i]);
	            }
	            scanner.close();
	        } catch (FileNotFoundException error) {
	            error.printStackTrace();
	        }
	        
	}
	
	public void updateLeaderBoard(String winner, int moves){
		
		JLabel name[] = new JLabel[5];
		JLabel score[] = new JLabel[5];
		int scores[] = new int[5];
		String names[] = new String[5];
		
		 File file = new File("leaderboard.txt");
		 int i =0;
	        try {
	 
	            Scanner scanner = new Scanner(file);
	 
	        for(i=0; i<5;i++){
	                names[i] = scanner.nextLine();
	                scores[i] = scanner.nextInt();
	                if(scanner.hasNextLine()){
	                	scanner.nextLine();
	                }
	        }
	            
	            scanner.close();
	        } catch (FileNotFoundException error) {
	            error.printStackTrace();
	        }
	        
	        
	        boolean newHighScore = false;
	        int newPos=0;
	        for(int j=0; j<5; j++){
	        	if(moves < scores[j]){
	        	newHighScore = true;
	        	newPos=j;
	        	break;
	        	}
	        }
	        
	        for(int k=4; k > newPos; k--){
	        	scores[k]=scores[k-1];
	        	names[k]=names[k-1];
	        }
	        
	        scores[newPos]=moves;
	        names[newPos]=winner;
	        
	        try{
	        	   // Create file 
	        	   FileWriter fstream = new FileWriter("leaderboard.txt");
	        	   BufferedWriter out = new BufferedWriter(fstream);
	        	   for(int m=0; m<5; m++){
	        	   out.write(names[m]+"\n");
	        	   out.write(scores[m]+"\n");
	        	   }
	        	   //Close the output stream
	        	   out.close();
	        	   }catch (Exception e){//Catch exception if any
	        	   System.err.println("Error: " + e.getMessage());
	        	   }
	        	 
	        
		
		
	}
	
	
	
	public void setVisible(Boolean t){
		
		if(t == false)
			leaderFrame.setVisible(false);
		else 
			leaderFrame.setVisible(true);
		
		
	}
	
}
